<?php
/*
登录确认页
*/

if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}

include 'init.php';
//ob_clean();
$method=trim($_POST['m']);
$account=trim($_POST['u']);

if($method=="getimg")
{
	//获取用户100头像
	$usr=new userModel();
	$img=$usr->getimg($account,100);
	if($img)
	{
		echo json_encode($img);
	}
	else
	{
		echo json_encode("no");
	}

}
else if($method=="login")
{
	//判断登录信息


	$pwd=trim($_POST['p']);
	$status=trim($_POST['s']);

	$usr=new userModel();
	$user=false;
	//如果是邮箱账号
	if(preg_match("/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/",$account))
	{
		$user=$usr->findEmail($account);
		if($pwd!=md5($user['pwd']))
		{
			echo json_encode('nopwd');
			exit;
		}

		//如果邮箱账号未获得验证
		if($user['email_validate']=='0')
		{
			//返回未验证信息并退出
			echo json_encode('novali');
			exit;
		}
	}
	else
	{
		$user=$usr->find($account);
	}
	

	
	if($user)
	{

		if($pwd==md5($user['pwd']))
		{
			//session_start();
			//$_SESSION['user']=$user;
			//将session加入memcache
			//$usr->addSeon($user['uid'],$_SESSION['user']);


			//1,每次登录时，将上次登录时间转为下线时间，将现在时间转为新到登录时间
			$usr->setlogout($user['id'],$user['lastlogin']);
			$usr->setlogin($user['id']);
			
			if($status<0||$status>6)
			{
				$status=1;
			}

			//2,更改在线状态
			$usr->changeStatus($user['id'],$status,30);
			

			//返回用户信息
			
			echo json_encode($user);

		}
		else
		{
			echo json_encode("nopwd");
		}
	}
	else
	{
		echo json_encode("nousr");
	}

}
else if($method=='findpwd')
{
	
	//60秒内不能发送第二次邮件
	//如果时间在60秒内，返回
	session_start();
	if(isset($_SESSION['mailtime'])&&(time()-$_SESSION['mailtime'])<60)
	{
		echo ('forbiden');
		exit;
	}
	

	$email=$_POST['e'];
	$usr=new userModel();
	$user=$usr->find($account);
	if($user)
	{
		if($user['email']==$email)
		{
			//发送邮件到此邮箱
			//创建一个邮件检验session
			
			$code=time().','.md5($user['email']).','.md5($user['name']);
			$_SESSION['findpwdcode']=$code;
			$_SESSION['usr']=$user['id'];
			//$_SESSION['account']=$user['account'];
			$_SESSION['email']=$user['email'];

			ini_set('SMTP','smtp.163.com');
			ini_set('smtp_port','25');
			
			require (ROOT.'tools/class.phpmailer.php');
			$mailer=new PHPMailer();
			$mailer->Charset='utf-8';
			$mailer->ContentType='text/html';
			$mailer->Encoding='base64';
			$mailer->SetLanguage('zh_cn');
			$mailer->IsSMTP();
			$mailer->Host='smtp.163.com';
			$mailer->Port=25;
			$mailer->SMTPAuth=true;
			$mailer->Username='a425661254@163.com';
			$mailer->Password='yzlbpbvwuwpcuybu';
			$mailer->FromName='肥扣扣在线聊天网';
			$mailer->From='a425661254@163.com';
			$mailer->AddAddress($user['email'],$user['name']);
			$mailer->Subject='找回密码';
			$cont="<center>
			<h2>您正在找回密码操作,请您在收到邮件30分钟内点击如下链接完成重设密码，过期无效</h2>
			<h3><a href='http://fakeqq.win/resetpwd.php?code=$code'>点击去重设密码>>></a></h3>
			<font color='red'>*如果您没有发送此类邮件，请不要理会*</font>
			</center>
										<div style='float:right;'>--肥扣扣在线</div>
			";
			$mailer->Body=$cont;
			if($mailer->Send())
			{
				//成功发送

				//重设限制时间值
				$_SESSION['mailtime']=time();
				echo ('ok');
				exit;
			}
			else
			{
				//邮件发送失败;
				echo ('mailerr');
				exit;
			}

		}
		else
		{
			//邮箱与账户不对称
			echo ('noemail');
			exit;
		}
	}
	else
	{
		//没有此用户
		echo ('nousr');
		exit;
	}

}
else if($method=='valimail')
{
	//60秒内不能发送第二次邮件
	//如果时间在60秒内，返回
	session_start();
	if(isset($_SESSION['mailtime'])&&(time()-$_SESSION['mailtime'])<60)
	{
		echo json_encode('forbiden');
		exit;
	}


}




